/**
 * 基于腾讯地图api获取位置信息
 */
export default class TxMapHandler {
  private baseUrl: string
  private key: string

  constructor (key?: string) {
    this.baseUrl = 'https://apis.map.qq.com'
    this.key = key || import.meta.env.VITE_KEY_TXMAP
  }

  reverseGeocoder = async () => {
    const _this = this
    return new Promise((resolve, reject) => {
      uni.getLocation({
        type: 'gcj02',
        success (res) {
          const { latitude, longitude } = res
          uni.request({
            url: `${_this.baseUrl}/ws/geocoder/v1/`,
            data: {
              location: `${latitude},${longitude}`,
              key: _this.key,
            },
            success (res) {
              resolve(res.data)
            },
            fail (err) {
              reject(err)
            },
          })
        },
        fail (err) {
          reject(err)
        },
      })
    })
  }
}
